'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview vip等级
 * @author sundbcn@gmail.com (sun debing)
 */

var moment = require('moment');
var modelUtil = require('../util/modelUtil');
var constantUtil = require('../util/constantUtil');
var mongoConnector = require('../libs/mongodb/mongoConnector');
/* jshint unused:false */
var unionHelpSchema = require('../schemas/unionHelpSchema.js');
var Help = mongoConnector().model('Help');
var haveHelpModel = require('../../app/models/haveHelpModel.js');

/**
 * 根据role_id获取model
 * @return {[type]} [description]
 */
module.exports.getById = function (union_id, role_id, share_id, help_type, cb) {
    Help.findOne({
        union_id: union_id,
        role_id: role_id,
        share_id: share_id,
        help_type: help_type
    }, function (err, union_group_model) {
        if (!!err) {
            console.error(err);
            return cb();
        }
        cb(null, union_group_model);
    });
};

/**
 * 增加信息
 */
module.exports.createHelp = function (help_info, cb) {
    Help.remove({
        union_id: help_info.union_id,
        role_id: help_info.role_id,
        share_id: help_info.share_id,
        help_type: help_info.help_type
    }, function (err) {
        Help.create({
            union_id: help_info.union_id,
            role_id: help_info.role_id,
            job: help_info.job,
            name: help_info.name,
            share_id: help_info.share_id,
            end_time: help_info.end_time,
            help_type: help_info.help_type,
            lv: help_info.lv,
            help_max_num: help_info.help_max_num,
            help_max_times: help_info.help_max_times
        }, cb);
    });
};

/**
 * 根据联盟id查找list多个
 */
module.exports.findUnionHelpList = function (union_id, cb) {
    Help.find({
        union_id: union_id
    }).exec(cb);
};

/**
 * 根据联盟固定条件查找帮助信息
 */
module.exports.findUnionHelpItem = function (help_info, cb) {
    Help.findOne({
        union_id: help_info.union_id,
        role_id: help_info.role_id,
        share_id: help_info.share_id,
        help_type: help_info.help_type
    }).exec(cb);
};

/**
 * 移除信息
 */
module.exports.removeHelp = function (help_info, cb) {
    Help.remove({
        union_id: help_info.union_id,
        role_id: help_info.role_id,
        share_id: help_info.share_id,
        help_type: help_info.help_type
    }, cb);
};

// 更新数据
module.exports.refreshHelpData = function (help_info, only_id, cb) {
    var max_num = help_info.help_max_num - 1;
    Help.update({
        union_id: help_info.union_id,
        role_id: help_info.role_id,
        share_id: help_info.share_id,
        help_type: help_info.help_type
    }, {
        $set: {
            help_max_num: max_num,
        }
    }, cb);
};
